#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Create by zhang
# Create on 2022/6/22 14:43
from domain.report.common.period_enum import PeriodEnum
from domain.report.common.str_util import del_html_element
from domain.report.dto.profit_dto import ProfitDTO, ProfitSchema
from typing import List


_value_index_name = ["营业总收入(元)", "营业收入(元)", "金融资产利息收入(元)", "已赚保费(元)", "手续费及佣金收入(元)", "营业总成本(元)",
                     "营业成本(元)", "金融资产利息支出(元)", "手续费及佣金支出(元)", "退保金(元)", "赔付支出净额(元)",
                     "提取保险合同准备金净额(元)", "保单红利支出(元)", "分保费用(元)", "营业税金及附加(元)", "销售费用(元)",
                     "管理费用(元)", "财务费用(元)", "资产减值损失(元)", "公允价值变动收益(元)", "投资收益(元)",
                     "对联营企业和合营企业的投资收益(元)", "汇兑收益(元)", "营业利润(元)", "营业外收入(元)", "营业外支出(元)",
                     "非流动资产处置净损失(元)", "利润总额(元)", "所得税(元)", "净利润(元)", "归属于母公司所有者的净利润(元)",
                     "少数股东损益(元)", "基本每股收益(元)", "稀释每股收益(元)" ]


def xbrl2dtos(data, stock_code, period:PeriodEnum) -> List[ProfitDTO]:
    columns = data["columns"]
    rows = data["rows"]
    dto_list = list()
    for i in range(len(columns)):
        dto = ProfitDTO()
        dto.stock_code:str = stock_code
        dto.stock_id = int(stock_code.split(".")[0])
        dto.period = period
        dto.year = int(del_html_element(data["columns"][i]["title"]))
        for j in range(len(data["rows"])):
            val = del_html_element(rows[j][del_html_element(columns[i]["field"])])
            val = val.replace("-", "") if val and "-" in val else val
            ProfitSchema.set(dto, _value_index_name[j], val)
        dto_list.append(dto)
    return dto_list
